package demo.aop;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * 广州奥凯信息咨询有限公司
 * <p>
 * 本源代码由《项目一组》及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (广州奥凯信息咨询有限公司) 2019-2022. all rights reserved.
 */

public class ClassProxy {
    public static void main(String[] args) {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "target\\class\\cglib");
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(ClassParent.class);
        enhancer.setInterfaces(new Class[]{Serializable.class, DemoInterface.class});
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                if (method.getName().equals("echo")){
                    return args[0];
                }
                return proxy.invokeSuper(obj, args);
            }
        });
        ClassParent o = (ClassParent) enhancer.create();
        System.out.println(o.echo("maikec"));
        o.setName("maikec001");
        System.out.println(o.getName());

    }
}
abstract class ClassParent implements DemoInterface{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
interface DemoInterface{
    String echo(String echo);
}
